<?php
namespace App\Helpers;
use AlibabaCloud\SDK\Bailian\V20231229\Bailian;
use AlibabaCloud\SDK\Bailian\V20231229\Models\AddFileRequest;
use AlibabaCloud\SDK\Bailian\V20231229\Models\ApplyFileUploadLeaseRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use GuzzleHttp\Client;

class AiFileUploadHelper
{
    public static function createClient(){
        $config = new Config([
            "accessKeyId" => 'LTAI5t9NLYTfQckEzTmYxEEX',
            "accessKeySecret" => 'XN0XvUL4m1MXQgtG779Jh7ybHrvt7H'
        ]);
        $config->endpoint = "bailian.cn-beijing.aliyuncs.com";
        return new Bailian($config);
    }
    public static function ApplyFileUploadLease($filepath){
        $client = self::createClient();
        $applyFileUploadLeaseRequest = new ApplyFileUploadLeaseRequest([
            'fileName' => basename($filepath),
            'md5' => md5_file($filepath),
            'sizeInBytes' => filesize($filepath),
            "categoryType" => "SESSION_FILE"
        ]);
        $runtime = new RuntimeOptions([
            "ignoreSSL" => true
        ]);
        $headers = [];
        $response = $client->applyFileUploadLeaseWithOptions("default", "llm-ubwv25mol5cfdm27", $applyFileUploadLeaseRequest, $headers, $runtime);
        //获取返回结果
        return $response->body->data;
    }
    public static function uploadFile($preSignedUrl, $filePath,$headers) {
        $client = new Client([
            'verify' => false
        ]);
        $fileContent = file_get_contents($filePath);
        // 发送PUT请求上传文件
        $response = $client->put($preSignedUrl, [
            'headers' => $headers,
            'body' => $fileContent
        ]);
        if ($response->getStatusCode() == 200) {
            return true;
        } else {
            throw  new \Exception("Failed to upload the file. ResponseCode: " . $response->getStatusCode());
        }
    }
    public static function AddFile($leaseId){
        $client = self::createClient();
        $addFileRequest = new AddFileRequest([
            "leaseId" => $leaseId,
            "parser" => "DASHSCOPE_DOCMIND",
            "categoryId" => "default",
            "categoryType" => "SESSION_FILE"
        ]);
        $runtime = new RuntimeOptions([
            "ignoreSSL" => true
        ]);
        $headers = [];
        // 复制代码运行请自行打印 API 的返回值
        $response = $client->addFileWithOptions("llm-ubwv25mol5cfdm27", $addFileRequest, $headers, $runtime);
        return $response->body->data->fileId;
    }

    public static function CheckFile($fileId)
    {
        $client = self::createClient();
        $runtime = new RuntimeOptions([
            "ignoreSSL" => true
        ]);
        $headers = [];
            $res = $client->describeFileWithOptions("llm-ubwv25mol5cfdm27", $fileId, $headers, $runtime);
            if(isset($_GET['debug'])){
                return $res->body;
            }
            return $res->body->data->status ?? 'INDEX_BUILDING_PENDING';
        try {
            $res = $client->describeFileWithOptions("llm-ubwv25mol5cfdm27", $fileId, $headers, $runtime);
			\Log::info($res->body);
            return $res->body->data->status;
        }
        catch (\Throwable $error) {
            \Log::error($error->getMessage());
            return 'error';
        }
    }
}
